//
//  KKListViewProtocolManager.m
//  KKListViewDemo
//
//  Created by liaozhenming on 16/11/4.
//  Copyright © 2016年 liaozhenming. All rights reserved.
//

#import "KKListViewProtocolManager.h"
#import "UIViewController+List.h"

@implementation KKListViewProtocolManager

+ (instancetype)kk_listViewProtocolManagerWithSectionItems:(NSMutableArray*)sectionItems {
    
    KKListViewProtocolManager *manager = [KKListViewProtocolManager new];
    manager.kk_sectionItems = sectionItems;
    return manager;
}

#pragma mark - UITableView methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.kk_sectionItems.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    KKListViewSectionItem *sectionItem = self.kk_sectionItems[section];
    return sectionItem.kk_cellItems.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    KKListViewSectionItem *sectionItem = self.kk_sectionItems[section];
    return sectionItem.sectionHeaderSize.height;
}

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    KKListViewSectionItem *sectionItem = self.kk_sectionItems[section];
    if (sectionItem.sectionHeaderClassName == nil) {
        return nil;
    }
    
    KKTableViewHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier: sectionItem.sectionHeaderClassName];
    if ([headerView respondsToSelector: @selector(setKk_sectionItem:)]) {
        headerView.kk_sectionItem = sectionItem;
    }
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    KKListViewSectionItem *sectionItem = self.kk_sectionItems[section];
    return sectionItem.sectionFooterSize.height;
}

- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    KKListViewSectionItem *sectionItem = self.kk_sectionItems[section];
    if (sectionItem.sectionHeaderClassName == nil) {
        return nil;
    }
    
    KKTableViewFooterView *footerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier: sectionItem.sectionFooterClassName];
    if ([footerView respondsToSelector: @selector(setKk_sectionItem:)]) {
        footerView.kk_sectionItem = sectionItem;
    }
    return footerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    KKListViewSectionItem *sectionItem = self.kk_sectionItems[indexPath.section];
    KKListViewCellItem *cellItem = sectionItem.kk_cellItems[indexPath.row];
    return cellItem.kk_rowHeight;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    KKListViewSectionItem *sectionItem = self.kk_sectionItems[indexPath.section];
    KKListViewCellItem *cellItem = sectionItem.kk_cellItems[indexPath.row];
    KKTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: cellItem.kk_cellClassName forIndexPath: indexPath];
    if ([cell respondsToSelector: @selector(setKk_cellItem:)]) {
        cell.kk_cellItem = cellItem;
    }
    cell.textLabel.text = [NSString stringWithFormat: @"%@-%@",@(indexPath.section),@(indexPath.row)];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    KKListViewSectionItem *sectionItem = self.kk_sectionItems[indexPath.section];
    KKListViewCellItem *cellItem = sectionItem.kk_cellItems[indexPath.row];
    if (cellItem.selectedEventHandle) {
        cellItem.selectedEventHandle();
    }
}


#pragma mark - UICollectionView methods
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return self.kk_sectionItems.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    KKListViewSectionItem *sectionItem = self.kk_sectionItems[section];
    return sectionItem.kk_cellItems.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    KKListViewSectionItem *sectionItem = self.kk_sectionItems[section];
    return sectionItem.sectionHeaderSize;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    KKListViewSectionItem *sectionItem = self.kk_sectionItems[section];
    return sectionItem.sectionFooterSize;
}

- (UICollectionReusableView*)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    KKListViewSectionItem *sectionItem = self.kk_sectionItems[indexPath.section];
    
    if ([kind isEqualToString: UICollectionElementKindSectionHeader]) {
        KKCollectionHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind: kind withReuseIdentifier:sectionItem.sectionHeaderClassName forIndexPath: indexPath];
        if ([headerView respondsToSelector: @selector(setKk_sectionItem:)]) {
            headerView.kk_sectionItem = sectionItem;
        }
        return headerView;
    }
    else {
        KKCollectionFooterView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind: kind withReuseIdentifier:sectionItem.sectionFooterClassName forIndexPath: indexPath];
        if ([footerView respondsToSelector: @selector(setKk_sectionItem:)]) {
            footerView.kk_sectionItem = sectionItem;
        }
        return footerView;
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    KKListViewSectionItem *sectionItem = self.kk_sectionItems[indexPath.section];
    KKListViewCellItem *cellItem = sectionItem.kk_cellItems[indexPath.row];
    return cellItem.kk_cellSize;
}

- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    KKListViewSectionItem *sectionItem = self.kk_sectionItems[indexPath.section];
    KKListViewCellItem *cellItem = sectionItem.kk_cellItems[indexPath.row];
    KKCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier: cellItem.kk_cellClassName forIndexPath: indexPath];
    
    if ([cell respondsToSelector: @selector(setKk_cellItem:)]) {
        cell.kk_cellItem = cellItem;
    }
    return cell;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    
    KKListViewSectionItem *sectionItem = self.kk_sectionItems[section];
    return sectionItem.sectionInsets;
}

@end
